package com.etc.controller;

import com.etc.pojo.News;
import com.etc.pojo.User;
import com.etc.service.IUserService;
import com.etc.service.NewsDaoServices;
import com.github.pagehelper.PageInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/UserManageController")

public class UserManageController {
    //调用业务层
    @Autowired
    private IUserService service;
    @Autowired
    private NewsDaoServices newsService;
    Logger logger= Logger.getLogger(UserManageController.class);

    /**
     * 分页模糊查询用户
     * @param pageNum 页数
     * @param size  每页显示的条数
     * @param userName  关键词
     * @param session
     * @return
     */
    @RequestMapping("/findUsers")
    @ResponseBody
    public PageInfo<User> findUsersPage(String userName, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "8")int size, HttpSession session){
       ModelAndView mv=new ModelAndView();
       if (userName!=null){
           session.setAttribute("keyword",userName);
       }else {
           userName=(String) session.getAttribute("keyword");
       }
        List<User> users=service.getPageUsers(userName,pageNum,size);
        PageInfo<User> pageInfo=new PageInfo<User>(users);
        mv.setViewName("showUsers");
        mv.addObject("pageInfo",pageInfo);
        return pageInfo;
    }

    /**
     * 增加用户
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public String add(User user){
        service.add(user);
        return "redirect:/findUsers";
    }

   /* @RequestMapping(value = "/checkName", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String checkName(String userName) {
        User user = service.queryUserByName(userName);
        if (user != null) {
            return "用户名已存在";
        }
        return null;
    }

    @RequestMapping(value = "/checkTel", produces = "text/html; charset=utf-8")
    @ResponseBody
    public String checkTel(String userTel) {
        User user = service.queryUserByTelephone(userTel);
        System.out.println(user);
        if (user != null) {
            return "手机号已存在";
        }
        return null;
    }*/
    /**
     * 修改用户状态
     */
    @RequestMapping("/startUser")
    @ResponseBody
    public void startUser(int userId){
        service.startUser(userId);
    }

    @RequestMapping("/logoutUser")
    @ResponseBody
    public void logoutUser(int userId){
        service.logoutUser(userId);
    }

    /**
     * 分页模糊查询动态
     * @param pageNum 页数
     * @param size  每页显示的条数
     * @param userId   关键词
     * @param newsDetail 关键词2
     * @param session
     * @return
     */
    @RequestMapping("/findNewsByLike")
    @ResponseBody
    public PageInfo<News> findNewsByLike(String userId, String newsDetail, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "8")int size, HttpSession session, HttpServletRequest request){
        System.out.println("用户id:"+userId);
        int uid;
        if (userId ==null || userId.equals("")){
          uid=0;
        }else {
            uid=Integer.parseInt(userId);
        }

        List<News> news=newsService.getNewsByLike(uid,newsDetail,pageNum,size);
        PageInfo<News> pageInfo=new PageInfo<News>(news);
        return pageInfo;
    }

}
